package com.cz.basetool.ui_work.util.image;

import android.os.Environment;
import android.util.Base64;
import android.util.Log;

import com.cz.basetool.ui_work.util.log.AndroidConsoleLogPrinter;
import com.cz.basetool.ui_work.util.txt.CZ_TextUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;

public class PhotoFileUtils {
    private static final String TAG = "PhotoFileUtils";
    public static String SDPATH = Environment.getExternalStorageDirectory() + File.separator + "com.juli.guangxi.pos/publish/";
    public static String SDPATH_EMPTY = Environment.getExternalStorageDirectory() + File.separator + "com.juli.guangxi.pos/empty/";


    /**
     * 27      * 将图片转换成Base64编码
     * 28      * @param imgFile 待处理图片
     * 29      * @return
     * 30
     */
    public static String getImgBase64Str(String imgFile) {
        //将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CZ_TextUtil.replaceBlank(new String(Base64.encodeToString(data,Base64.DEFAULT)));
    }

    public static File createDir(String dirName) throws IOException {
        File dir = new File(dirName);
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            System.out.println("createDir:" + dir.getAbsolutePath());
        }
        if (!dir.exists()) {
            boolean mkdirs = dir.mkdirs();
            if(mkdirs == false){
                AndroidConsoleLogPrinter.e(TAG,"创建文件夹" + dir.getAbsolutePath() + "失败");
            }
        }
        return dir;
    }

    public static File createFile(String dirName) throws IOException {
        File file = new File(dirName);
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            System.out.println("createFile:" + file.getAbsolutePath());
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        return file;
    }


    public static void delFile(String fileName) {
        File file = new File(SDPATH + fileName);
        if (file.isFile()) {
            file.delete();
        }
        file.exists();
    }

    public static void deleteDir(String path) {
        File dir = new File(path);
        if (dir == null || !dir.exists()) {
            return;
        }
        if(dir.isDirectory()){
            for (File file : dir.listFiles()) {
                if (file.isFile()) {
                    file.delete(); // 删除所有文件
                } else if (file.isDirectory()) {
                    deleteDir(path); // 递规的方式删除文件夹
                }
            }
        }
        dir.delete();// 删除目录本身
    }

    public static boolean fileIsExists(String path) {
        try {
            File f = new File(path);
            if (!f.exists()) {
                return false;
            }
        } catch (Exception e) {

            return false;
        }
        return true;
    }


    public static final int SIZETYPE_B = 1;//获取文件大小单位为B的double值
    public static final int SIZETYPE_KB = 2;//获取文件大小单位为KB的double值
    public static final int SIZETYPE_MB = 3;//获取文件大小单位为MB的double值
    public static final int SIZETYPE_GB = 4;//获取文件大小单位为GB的double值

    /**
     * 转换文件大小,指定转换的类型
     *
     * @param fileS
     * @param sizeType
     * @return
     */
    public static double formatFileSize(long fileS, int sizeType) {
        DecimalFormat df = new DecimalFormat("#.00");
        double fileSizeLong = 0;
        switch (sizeType) {
            case SIZETYPE_B:
                fileSizeLong = Double.valueOf(df.format((double) fileS));
                break;
            case SIZETYPE_KB:
                fileSizeLong = Double.valueOf(df.format((double) fileS / 1024));
                break;
            case SIZETYPE_MB:
                fileSizeLong = Double.valueOf(df.format((double) fileS / 1048576));
                break;
            case SIZETYPE_GB:
                fileSizeLong = Double.valueOf(df.format((double) fileS / 1073741824));
                break;
            default:
                break;
        }
        return fileSizeLong;
    }


    public static void inputstream2file(InputStream ins,File file){
        File parentFile = file.getParentFile();
        OutputStream os = null;
        try {
            PhotoFileUtils.createDir(parentFile.getAbsolutePath());
            PhotoFileUtils.createFile(file.getAbsolutePath());
            os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[1024];
            while ((bytesRead = ins.read(buffer)) != -1) {
                os.write(buffer, 0, bytesRead);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(ins != null){
                try {
                    ins.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
